package UserClient;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JOptionPane;
public class WelcomeJFrame extends javax.swing.JFrame {

	private static final int NULL = 0;

	/**
	 * Creates new form Login
	 */
	public WelcomeJFrame() {
		initComponents();
		super.setTitle("彩票购买抽奖程序");
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		super.setLocation((width - 400) / 2, (height - 350) / 2);
	}

	private void initComponents() {

		WelcomeLabel = new javax.swing.JLabel();
		PasswordLabel = new javax.swing.JLabel();
		UsernameField = new javax.swing.JTextField();
		PasswordField = new javax.swing.JPasswordField();
		UsernameLabel = new javax.swing.JLabel();
		EnrollButton = new javax.swing.JButton();
		LoginButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		WelcomeLabel.setFont(new java.awt.Font("宋体", 1, 36)); // NOI18N
		WelcomeLabel.setText("WELCOME");

		PasswordLabel.setText("Password：");

		UsernameField.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				UsernameFieldActionPerformed(evt);
			}
		});

		PasswordField.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				PasswordFieldActionPerformed(evt);
			}
		});

		UsernameLabel.setText("Username：");

		EnrollButton.setText("注册");
		EnrollButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				EnrollButtonActionPerformed(evt);
			}
		});

		LoginButton.setText("登录");
		LoginButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					LoginButtonActionPerformed(evt);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
				.createSequentialGroup().addContainerGap(65, Short.MAX_VALUE)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
								.addComponent(WelcomeLabel).addGap(91, 91, 91))
						.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
										.addComponent(EnrollButton)
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(UsernameLabel).addComponent(
														PasswordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 66,
														javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(UsernameField, javax.swing.GroupLayout.Alignment.TRAILING,
												javax.swing.GroupLayout.PREFERRED_SIZE, 105,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(PasswordField, javax.swing.GroupLayout.Alignment.TRAILING,
												javax.swing.GroupLayout.PREFERRED_SIZE, 105,
												javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(79, 79, 79))
						.addGroup(layout.createSequentialGroup().addGap(103, 103, 103).addComponent(LoginButton)
								.addContainerGap(100, Short.MAX_VALUE)))));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup().addContainerGap().addComponent(WelcomeLabel).addGap(18, 18, 18)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(UsernameField, javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(UsernameLabel))
						.addGap(18, 18, 18)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(PasswordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(EnrollButton).addComponent(LoginButton))
						.addGap(20, 20, 20)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void UsernameFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_UsernameFieldActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_UsernameFieldActionPerformed

	private void EnrollButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_EnrollButtonActionPerformed
		new EnrollJFrame().setVisible(true);
		this.setVisible(false);
	}// GEN-LAST:event_EnrollButtonActionPerformed

	private void PasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_PasswordFieldActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_PasswordFieldActionPerformed

	private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt)
			throws SQLException, ClassNotFoundException {// GEN-FIRST:event_LoginButtonActionPerformed
		String name = UsernameField.getText();
		String string = Arrays.toString(PasswordField.getPassword());
		Class.forName(driverName);
		connection = DriverManager.getConnection(url, userName, password);
		String sql = "select * from userreserve where username = ?;";
		connection.setAutoCommit(false);
		PreparedStatement prepare = connection.prepareStatement(sql);
		prepare.setString(1, name);
		System.out.println(prepare);
		resultSet = prepare.executeQuery();
		boolean flag = false;
		int[] num = new int[7];
		while (resultSet.next()) {
			flag = true;
			if (resultSet.getString(3).equals(string)) {
				User.setUsername(resultSet.getString(2));
				User.setMoney(resultSet.getString(4));
				User.setTelnum(resultSet.getString(5));
				new PurchaseJFrame().setVisible(true);
				this.setVisible(false);
				if (resultSet.getInt(6) != NULL) {

					for (int i = 0; i < 7; i++) {
						num[i] = resultSet.getInt(i + 6);
						System.out.println(num[i]);
					}
					User.setNotes(num);
					for (int i = 0; i < num.length; i++) {
						System.out.println("vsdvs"+User.getNotes()[i]);
						
					}
				}
				break;
			} else {
				JOptionPane.showMessageDialog(null, "密码不正确，请重新输入");
			}

		}
		if (!flag) {
			JOptionPane.showMessageDialog(null, "用户不存在！");

		}
		int times = resultSet.getInt(13);
		System.out.println(times);
		// ---------------
		if (num.length != 0) {
			System.out.println("你已经买过彩票了");
			// 说明已买过彩票
			String sql1 = "select * from winmes;"; // 获取奖池的号码
			String sql2 = "update userreserve set money =? where username=?;";
			statement = connection.createStatement();//
			resultSet = statement.executeQuery(sql1);
			resultSet.next();
			int shot = 0;
			// for (int i = 0; i < num.length; i++) {
			// System.out.println(num[i]);
			// }
			for (int i = 0; i < 7; i++) {
				for (int j = 0; j < 7; j++) {
					if (num[i] == resultSet.getInt(j + 2)) {
						shot++;
						System.out.println("shot被加了");
					}
				}
				User.setNotes(num);
				System.out.println(num[i]);//
			}
			prepare = connection.prepareStatement(sql2);

			if (shot == 6) {
				User.setMoney(Integer.parseInt(User.getMoney()) + 1000 * times + ""); // 奖金
				prepare.setString(1, User.getMoney());
				prepare.setString(2, User.getUsername());
				prepare.executeBatch();
				connection.commit();
				JOptionPane.showMessageDialog(null, "恭喜获得一等奖！");
			}
			if (shot == 7) {
				User.setMoney(User.getMoney() + 2000 * times); // 奖金
				prepare.setString(1, User.getMoney());
				prepare.setString(2, User.getUsername());
				prepare.executeBatch();
				connection.commit();
				JOptionPane.showMessageDialog(null, "恭喜获得特等奖！");
			}
		}
	}// GEN-LAST:event_LoginButtonActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new WelcomeJFrame().setVisible(true);
			}
		});
	}

	private String url = "jdbc:mysql://localhost:3306/message?useUnicode=true&characterEncoding=GBK&useSSL=false";
	private String userName = "root";
	private String password = "552364871";
	private String driverName = "com.mysql.jdbc.Driver";
	private static Connection connection = null;
	private static Statement statement = null;
	private static ResultSet resultSet = null;
	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton EnrollButton;
	private javax.swing.JButton LoginButton;
	private javax.swing.JPasswordField PasswordField;
	private javax.swing.JLabel PasswordLabel;
	private javax.swing.JTextField UsernameField;
	private javax.swing.JLabel UsernameLabel;
	private javax.swing.JLabel WelcomeLabel;
	// End of variables declaration//GEN-END:variables
}
